package math.geom3d;

import math.base.CCS3d;
import math.base.Point3d;
import math.base.Vector3d;
import math.geomUtils.CurveType;

/**
 * @author Saga
 *
 */
public class Circle3d extends ConicalCurve3d {
    public Circle3d(final CCS3d _pos, final double _radius) {
        this.pos = _pos;
        this.radius = _radius;
    }

    @Override
    public final CurveType getCurveType() {
        return CurveType.CIRCLE;
    }

    @Override
    public final void reverse() {
        this.pos.yReverse();
    }

    @Override
    public final Curve3d reversed() {
        return new Circle3d(this.pos.clone(), this.radius);
    }

    @Override
    public final boolean isPeriodic() {
        return true;
    }

    @Override
    public final double getPeriod() {
        return Math.PI * 2.0;
    }

    @Override
    public final double getFirstParameter() {
        return 0.0;
    }

    @Override
    public final double getLastParameter() {
        return Math.PI * 2.0;
    }

    @Override
    public final Point3d calcValue(final double _param) {
        final double cosVal = Math.cos(_param) * this.radius;
        final double sinVal = Math.sin(_param) * this.radius;
        return this.pos.getOrigin().added(this.pos.getDirectX().multiplied(cosVal)).added(
                this.pos.getDirectY().multiplied(sinVal));
    }

    @Override
    public final Vector3d calcDerivative(final double _param) {
        final double cosVal = Math.cos(_param) * this.radius;
        final double sinVal = Math.sin(_param) * this.radius;
        return this.pos.getDirectX().multiplied(-sinVal).added(
                this.pos.getDirectY().multiplied(cosVal));
    }

    @Override
    public final Vector3d calcDerivativeN(final double _param, final int _order) {
        if (_order < 1) {
            throw new ArithmeticException("");
        }

        final double cosVal = Math.cos(_param) * this.radius;
        final double sinVal = Math.sin(_param) * this.radius;

        switch (_order % 4) {
        case 0:
            return this.pos.getDirectX().multiplied(cosVal).added(
                    this.pos.getDirectY().multiplied(sinVal));

        case 1:
            return this.pos.getDirectX().multiplied(-sinVal).added(
                    this.pos.getDirectY().multiplied(cosVal));

        case 2:
            return this.pos.getDirectX().multiplied(-cosVal).added(
                    this.pos.getDirectY().multiplied(-sinVal));

        case 3:
            return this.pos.getDirectX().multiplied(sinVal).added(
                    this.pos.getDirectY().multiplied(-cosVal));

        default:
            throw new ArithmeticException("");
        }
    }

    @Override
    public final void calcValueAndDerivative1(final double _param, final Point3d _pt,
            final Vector3d _der1) {
        final double cosVal = Math.cos(_param) * this.radius;
        final double sinVal = Math.sin(_param) * this.radius;
        _pt.setCoord(this.pos.getOrigin().getCoord()
                .added(this.pos.getDirectX().getCoord().multiplied(cosVal))
                .added(this.pos.getDirectY().getCoord().multiplied(sinVal)));
        _der1.setCoord(this.pos.getDirectX().getCoord().multiplied(-sinVal)
                .added(this.pos.getDirectY().getCoord().multiplied(cosVal)));
    }

    @Override
    public final void calcValueAndDerivative2(final double _param, final Point3d _pt,
            final Vector3d _der1, final Vector3d _der2) {
        final double cosVal = Math.cos(_param) * this.radius;
        final double sinVal = Math.sin(_param) * this.radius;
        _pt.setCoord(this.pos.getOrigin().getCoord()
                .added(this.pos.getDirectX().getCoord().multiplied(cosVal))
                .added(this.pos.getDirectY().getCoord().multiplied(sinVal)));
        _der1.setCoord(this.pos.getDirectX().getCoord().multiplied(-sinVal)
                .added(this.pos.getDirectY().getCoord().multiplied(cosVal)));
        _der2.setCoord(this.pos.getDirectX().getCoord().multiplied(-cosVal)
                .added(this.pos.getDirectY().getCoord().multiplied(-sinVal)));
    }

    @Override
    public final void calcValueAndDerivative3(final double _param, final Point3d _pt,
            final Vector3d _der1, final Vector3d _der2, final Vector3d _der3) {
        final double cosVal = Math.cos(_param) * this.radius;
        final double sinVal = Math.sin(_param) * this.radius;
        _pt.setCoord(this.pos.getOrigin().getCoord()
                .added(this.pos.getDirectX().getCoord().multiplied(cosVal))
                .added(this.pos.getDirectY().getCoord().multiplied(sinVal)));
        _der1.setCoord(this.pos.getDirectX().getCoord().multiplied(-sinVal)
                .added(this.pos.getDirectY().getCoord().multiplied(cosVal)));
        _der2.setCoord(this.pos.getDirectX().getCoord().multiplied(-cosVal)
                .added(this.pos.getDirectY().getCoord().multiplied(-sinVal)));
        _der3.setCoord(this.pos.getDirectX().getCoord().multiplied(sinVal)
                .added(this.pos.getDirectY().getCoord().multiplied(-cosVal)));
    }

    @Override
    public final Circle3d clone() {
        return null;
    }

    private final double radius;
}
